// pages/my/my.js
import {
    getbaseUrl,
    requestUtil
} from "../../utils/request"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        dialogShow:false,
        userImg:"",
        defaultImg:"avator.png",
        shopCard:[
            {
                id:1,
                icon:'icon-ly_huiyuanka',
                title:"超级吃货卡",
                titleName:'无门槛大额红包'
            },
            {
                id:2,
                icon:'icon-hongbao',
                title:"红包",
                titleName:'查看我的红包'
            },
            {
                id:3,
                icon:'icon-wodeyouhuiquan',
                title:"券包",
                titleName:'查看我的券包'
            },
            {
                id:4,
                icon:'icon-qiabao',
                title:"卡包",
                titleName:'查看会员卡'
            },
        ],
        moneyList:[
            {
                id:1,
                icon:"icon-shouye",
                title:"我的钱包"
            },
            {
                id:2,
               money:"10",
               total:"万",
                title:"借钱"
            },
            {  
                id:3,
                money:"420",
                total:"元",
                title:"外卖红包"
            },
            {
                id:4,
                money:"19",
                total:"元",
                title:"充值优惠"
            },
        ],


        infoList:[
            {
                id:1,
                icon:"icon-position-o",
                title:"我的地址"
            },
            {
                id:2,
                icon:"icon-kefu",
                title:"我的客服"
            },
            {  
                id:3,
                icon:"icon-gouwuche",
                title:"购物车"
            },
            {
                id:4,
                icon:"icon-guanzhu",
                title:"我的关注"
            },
            {
                id:5,
                icon:"icon-qiye",
                title:"企业订餐"
            },
            {
                id:6,
                icon:"icon-waimai",
                title:"邀友跑单"
            }, {
                id:7,
                icon:"icon-hezuo",
                title:"商务合作"
            },
            {
                id:8,
                icon:"icon-gongyi",
                title:"公益活动"
            },
            {
                id:9,
                icon:"icon-guize",
                title:"规则与资质"
            },
            {
                id:10,
                icon:"icon-shoucang",
                title:"评价中心"
            },
            {
                id:11,
                icon:"icon-fapiao",
                title:"发票助手"
            },
            {
                id:12,
                icon:"icon-zhiwu",
                title:"碳账户"
            },
        ],

        baoList:[
            {
                id:1,
                icon:"icon-jingyingfenxi",
                title:"经营分享"
            },
            {
                id:2,
                icon:"icon-yinsi",
                title:"隐私"
            },
            {
                id:3,
                icon:"icon-navicon-yhxy",
                title:"用户协议"
            },
            {
                id:4,
                icon:"icon-duihuanma",
                title:"兑换码"
            },
        ],

        userInfo: {},
        baseUrl: "",
        logout: '01.jpg',
        discountData: [],
        hei: 0,
        show: false,
        isPayPassword:0,
        
        warnning:"为了您的资金安全，请初始密码",
        submitPassword:0,
        plase:"",
        money:0
    },

    // 页面跳转
    changePage(e){
        // console.log(e.currentTarget.dataset.id)
        let id =e.currentTarget.dataset.id
        // console.log(id)
        if(id===1){
            this.Isshow()
        }else if(id===4){
            wx.navigateTo({
              url: '/pages/recharge/recharge',
            })
        }
    },

    // 跳去红包页
    toCard(e){
        let id =e.currentTarget.dataset.id
        if(id===1){
            this.setData({
                dialogShow:true
            })
            return
        }
        if(id===3||id===2){
            wx.navigateTo({
              url: '/pages/discount/discount',
            })
        }
        if(id===4){
            wx.navigateTo({
                url: '/pages/exchange/exchange',
              })
        }

        
    },

    // 去会员详情页
    tomember(){
        wx.navigateTo({
          url: '/pages/manber/manber',
        })
    },

    isLogin(){
        let openId = this.data.userInfo.openid
        if(!openId){
            wx.navigateTo({
              url: '/pages/login/login',
            })
        }else{
            this.setData({
                dialogShow:true
            })
        }


    },


    // 密码组件的回调
    submit(e){
        let openId = this.data.userInfo.openid
        let value = e.detail.value
        let submitPassword = this.data.submitPassword
        if(value!==submitPassword){
            this.setData({
                warnning:"请再次输入密码",
                submitPassword:value
            })
        }else{
            // console.log("密码一致")
            let res = requestUtil({
                url:"/shop/insertPaypassword",
                method:"get",
                data:{
                    openId,
                   password:submitPassword
                }
            })
            res.then(({data})=>{
                // console.log(data)
                if(data.status==200){
                    this.setData({
                        warnning:"为了您的资金安全，请再次输入初始密码",
                        show:false
                    })
                    this.getUserPayPassword()
                }else{
                    this.setData({
                        warnning:"为了您的资金安全，请初始密码",
                        plase:"密码初始化失败，请再试",
                        show:true,
                        submitPassword:0
                    })
                }
            })
          
        }
       
    },

    // 显示密码 or 去金额的详情页
    Isshow() {
        let isPayPassword = this.data.isPayPassword
        if (this.data.userInfo.openid) {
            // console.log("fjkoesf")
            if(isPayPassword==0){
                this.setData({
                    show: true
                })
            }else{
               wx.navigateTo({
                 url: '/pages/coin/coin',
               })
            }
    
        } else {
            this.toLogin()
        }

    },

    // 获取用户的密码
    getUserPayPassword() {
        let openId = this.data.userInfo.openid

        let res = requestUtil({
            url: "/shop/getUerPayPassword",
            method: "get",
            data: {
                openId
            }
        })

        res.then(({
            data
        }) => {
            // console.log(data)
            if(data.status==200){
                // console.log(data.data)
                this.setData({
                    isPayPassword:data.data.state
                })
            }else{
                this.setData({
                    isPayPassword:data.data
                })
            }
        })

    },

    // 获取用户的金额
    getUserMoney(){
        let openId = this.data.userInfo.openid
        let res = requestUtil({
            url:"/shop/getUserMoney",
            method:"get",
            data:{
                openId
            }
        })
        res.then(({data})=>{
            // console.log(data)
            let totalPrice = data.data.reduce((total,item)=>{
               return total += item.money
            },0)
            if(data.status==200){
                this.setData({
                    money:totalPrice.toFixed(2)
                })
            }else{
                this.setData({
                    money:0
                })
            }
        })
    },

    // 跳到个人页
    toPersonalData() {
        let openId = this.data.userInfo.openid
        let userName = this.data.userInfo.userName
        wx.navigateTo({
            url: `/pages/personalData/personalData?openId=${openId}&userName=${userName}`,
        })
    },

    // 跳到登录页
    toLogin() {
        // console.log("登录页")
        wx.navigateTo({
            url: '/pages/login/login',
        })
    },

    // 获取本地的用户信息

    getUserInfo() {
        wx.getStorage({
            key: "userInfo",
            success: (res) => {
                // res.data
                this.setData({

                    userInfo: JSON.parse(res.data)
                })
                this.getuserDiscount()
                this.getUserPayPassword()
                this.getUserMoney()
            },
            fail: () => {
                this.setData({
                    userInfo: {}
                })
            }

        })
    },

    // 跳转到优惠
    toDiscount() {
        // console.log("我爱你")
        if (this.data.userInfo.openid) {
            wx.navigateTo({
                url: '/pages/discount/discount',
            })
        } else {
            this.toLogin()
        }
    },

    // 跳到抽奖页
    toLuckdraw(){
        wx.navigateTo({
          url: '/pages/luckdraw/luckdraw',
        })
    },

    // 兑换优惠券
    exchange() {
        if (this.data.userInfo.openid) {
            wx.navigateTo({
                url: '/pages/exchange/exchange',
            })
        } else {
            this.toLogin()
        }
    },

    // 获取用户的优惠券

    getuserDiscount() {
        let openId = this.data.userInfo.openid
        // console.log(openId)
        let res = requestUtil({
            url: "/shop/getUserAddDiscount",
            method: "get",
            data: {
                openId
            }
        })
        res.then(({
            data
        }) => {
            // console.log(data)
            if (data.status == 200) {
                // 对比数据渲染

                this.setData({
                    discountData: data.data
                })
            } else {

                this.setData({
                    discountData: []
                })
            }
        })
    },

    // 获取高度
    gethei() {
        wx.getSystemInfo({
            success: (result) => {
                //   console.log(result.windowHeight)
                this.setData({
                    hei: result.windowHeight
                })
            },
        })
    },

       // 获取本地图片
       getStroeImg(){
        wx.getStorage({
            key:"userImg",
            success:(res)=>{
                // console.log(JSON.parse(res.data))
                let date =JSON.parse(res.data)
                this.setData({
                    userImg:date
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // console.log("我爱你")
        this.gethei()
        this.setData({
            baseUrl: getbaseUrl() + "/image/"
        })

    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        // console.log("我草你妈")
        this.getUserInfo()
        this.getStroeImg()


    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})